import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref<string | null>(null)
  const isLogin = ref(false)

  // 初始化状态
  const initializeState = () => {
    token.value = localStorage.getItem('token')
    isLogin.value = localStorage.getItem('isLogin') === 'true'
  }

  // 设置登录状态
  const setLoginState = (newToken: string) => {
    token.value = newToken
    isLogin.value = true
    localStorage.setItem('token', newToken)
    localStorage.setItem('isLogin', 'true')
  }

  // 清除登录状态
  const clearLoginState = () => {
    token.value = null
    isLogin.value = false
    localStorage.removeItem('token')
    localStorage.removeItem('isLogin')
  }

  return {
    token,
    isLogin,
    initializeState,
    setLoginState,
    clearLoginState
  }
}) 